package cn.lblbc.downloadlib.database;

import java.util.ArrayList;
import java.util.List;

import cn.lblbc.downloadlib.DownloadInfo;

/**
 * DownloadTaskEntity到DownloadInfo的映射器
 */
public class DownloadTaskEntityMapper {
    
    /**
     * 将DownloadTaskEntity转换为DownloadInfo
     * @param entity DownloadTaskEntity对象
     * @return DownloadInfo对象
     */
    public static DownloadInfo toDownloadInfo(DownloadTaskEntity entity) {
        if (entity == null) {
            return null;
        }
        
        DownloadInfo info = new DownloadInfo();
        info.setTaskId(entity.getTaskId());
        info.setPackageName(entity.getPackageName());
        info.setTotalSize(entity.getTotalSize() != null ? entity.getTotalSize() : 0);
        info.setDownloadedSize(entity.getDownloadedSize() != null ? entity.getDownloadedSize() : 0);
        info.setStatus(entity.getStatus());
        info.setWifiOnly(entity.getIsWifiOnly() == 1);
        info.setCreateTime(entity.getCreateTime());
        info.setUpdateTime(entity.getUpdateTime());
        info.setExtraInfo(entity.getExtraInfo());
        
        return info;
    }
    
    /**
     * 将DownloadTaskEntity列表转换为DownloadInfo列表
     * @param entities DownloadTaskEntity对象列表
     * @return DownloadInfo对象列表
     */
    public static List<DownloadInfo> toDownloadInfoList(List<DownloadTaskEntity> entities) {
        if (entities == null) {
            return new ArrayList<>();
        }
        
        List<DownloadInfo> infos = new ArrayList<>();
        for (DownloadTaskEntity entity : entities) {
            DownloadInfo info = toDownloadInfo(entity);
            if (info != null) {
                infos.add(info);
            }
        }
        
        return infos;
    }
}